<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class XcSite extends Model
{
    protected $table = 'xc_site';
    protected $guarded = ['id'];

    public function device()
    {
        return $this->hasOne('App\Models\XcDevice', 'site_id', 'id');
    }

    public function washmode()
    {
        return $this->hasMany('App\Models\XcSiteWashMode', 'site_id', 'id');
    }

    public function renter()
    {
        return $this->hasOne('App\Models\XcRenter', 'id', 'renter_id');
    }

    public function getRunningSite($time)
    {
        $res = DB::select("SELECT xc_site.* FROM xc_site LEFT JOIN xc_renter_contract ON  xc_site.renter_id = xc_renter_contract.renter_id WHERE DATE_FORMAT(xc_renter_contract.starttime, '%Y-%m') <= '{$time}' AND DATE_FORMAT(xc_renter_contract.endtime, '%Y-%m') >= '{$time}' and xc_renter_contract.id > 0 ");

        return $res;
    }
}
